from lxml import etree
import requests

# 设置请求头信息，伪装成浏览器进行访问
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

# 获取用户输入的需要翻译的单词
word = input("请输入要翻译的单词：")

# 发送请求
response = requests.post('https://fanyi.baidu.com/v2transapi', headers=headers, data={
    'from': 'en',
    'to': 'zh',
    'query': word,
    'transtype': 'realtime',
    'simple_means_flag': '3',
    'sign': '290405.114352',
    'token': '92011d06f07a0b102537ef67ace4875d'
})

# 解析html
html = etree.HTML(response.json()['trans_result']['data'][0]['dst'])

# 打印翻译结果
print('翻译结果：', html.xpath('string(.)'))
